﻿using Microsoft.IdentityModel.Tokens;
using Org.BouncyCastle.Asn1.X509;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WaterCloud.Code;
using WaterCloud.DataBase;
using WaterCloud.Domain.ContentManage;
using WaterCloud.Domain.Entity.STZBManage;
using static Serenity.Web.PropertyItemsScript;

namespace WaterCloud.Service.STZBManage
{
    /// <summary>
    /// 率土之滨服务类
    /// </summary>
    public class STZBService : BaseService<User>, IDenpendency
    {
        public STZBService(ISqlSugarClient context) : base(context)
        {
        }

        /// <summary>
        /// 注册账号
        /// </summary>
        /// <param name="name">账号</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        public async Task<int> EnrollAccount(string name, string pwd)
        {
            //检查账号是否存在
            var count = await repository.IQueryable().CountAsync(t => t.Account == name);
            if (count > 0) return -1; //账号已存在
            User user = new User
            {
                Id = Guid.NewGuid().ToString(),
                Account = name,
                Pwd = pwd,
                State = 0,
                CreateTime = DateTime.Now
            };
            await repository.Insert(user);
            return 1; //注册成功
        }

        /// <summary>
        /// 查询当前账号是否存在
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<User> Loggin_Account(string name, string pwd)
        {
            //查询当前账号密码是否存在
            var user = await repository.IQueryable(x => x.Account == name && x.Pwd == pwd).FirstAsync();
            return user;
        }


        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyword">关键词</param>
        /// <param name="stime">注册开始时间</param>
        /// <param name="etime">注册结束时间</param>
        /// <returns></returns>
        public async Task<List<User>> GetGridJson(SoulPage<User> pagination, string keyword, string stime, string etime)
        {
            DateTime sTime = DateTime.Now;
            DateTime eTime = DateTime.Now;
            if (!string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
            {
                sTime = DateTime.Parse(stime);
                eTime = DateTime.Parse(etime).AddHours(23).AddMinutes(59).AddSeconds(59);
            }
            var data = repository.IQueryable()
                .WhereIF(!string.IsNullOrEmpty(keyword), x => x.Account.Contains(keyword) || x.Pwd.Contains(keyword))
                .WhereIF(!string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime), x => x.CreateTime >= sTime && x.CreateTime <= eTime);
            //权限过滤（保证分页参数存在）
            data = GetDataPrivilege("x", "", data);
            return await data.ToPageListAsync(pagination);
        }

        /// <summary>
        /// 删除用户数据
        /// </summary>
        /// <param name="id">数据id,多个使用逗号隔开</param>
        /// <returns></returns>
        public async Task DeleteForm(string id)
        {
            var ids = id.Split(',');
            await repository.Delete(a => ids.Contains(a.Id));
        }
    }
}
